const express = require('express');

const app = express();


// 先配置CORS中间件
app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', '*');
    res.setHeader('Access-Control-Allow-Headers', '*');
    if (req.method === 'OPTIONS') {
        res.sendStatus(200);
        return;
    }
    next();
});

// 再配置解析中间件
app.use(express.urlencoded({extended:false}));
// 最后加载路由
const chatRoute = require('./routes/chatRoute');
app.use('/api', chatRoute);
//设置端口号
const port = process.env.PORT || 3000;

//静态资源
app.use(express.static('static'));
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});